package jscinttools.scint;

import jscinttools.utilities.jhplotutilities.FigureParametersJHPlot;
import jscinttools.utilities.jhplotutilities.HPlotJaUtil;
import jscinttools.utilities.aidautilities.AidaFileReader;
import jscinttools.utilities.aidautilities.GrabIObject;

import jscinttools.scint.statbox.StatBoxJa;
import jscinttools.scint.statbox.StatBoxSpec;

import java.lang.String;
import java.lang.System;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.File;

import jhplot.DrawOptions;
import jhplot.H1D;
import jhplot.F1D;
import jhplot.HPlotJa;
import jhplot.jadraw.JaText;
import jhplot.jadraw.JaAxes;

/**
 *  Description of the Class
 *
 *@author     Marc-Antoine Verdier
 *@created    3 avril 2011
 */
public class SpectrumPlotter implements FigureParametersJHPlot
{
	private HPlotJa plotter = null;

	private Spectrum spectrum = null;
	private StatBoxJa statBox = null;
	private String title = null;
	private String outPath = null;
	private boolean writeFigures = false;



	/**
	 *  Constructor for the SpectrumPlotter object
	 *
	 *@param  spectrum      Description of the Parameter
	 *@param  outPath       Description of the Parameter
	 *@param  title         Description of the Parameter
	 *@param  writeFigures  Description of the Parameter
	 */
	public SpectrumPlotter(Spectrum spectrum, String title, String outPath, boolean writeFigures)
	{
		this.spectrum = spectrum;
		this.title = title;
		this.outPath = outPath;
		this.writeFigures = writeFigures;
		this.statBox = new StatBoxSpec(spectrum);

		JaText prelim = new JaText("Preliminary", 0.175, 0.2, "NDC");
		prelim.setFont(titleFont);
		prelim.setColor(colors[1]);

		this.plotter = new HPlotJa("Spectrum", 600, 600, 1, 1, true);
		plotter.setGTitle(title, titleFont);
		plotter.setAntiAlias(false);
		if (!writeFigures)
		{
			plotter.visible(true);
		}
		else
		{
			plotter.visible(false);
		}
		plotter.cd(1, 1);
		plotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75);
		plotter.setRange(spectrum.getSpectrum().getMin(), spectrum.getSpectrum().getMax(), 0.0, 1.1);
		//plotter.setRange(0.0, 200.0, 0.0, 1.1);
		plotter.setLogScale(0, false);
		plotter.setLogScale(1, false);
		plotter.setTicFont(ticFont);
		plotter.setShowStatBox(false);
		plotter.showKey(false);
		plotter.setNameX("Number of Photons");
		plotter.setNameY("Normalized Counts (a.u.)");

		plotter.cd(1, 1);
		plotter.draw(spectrum.getSpectrum(), 1, 1);
		plotter.draw(spectrum.getFitFunc(), 1, 1);
		for (int i = 0; i < statBox.getStatBox().length; i++)
		{
			plotter.draw(statBox.getStatBox(i));
		}
		plotter.draw(prelim);

		JaAxes theAxes_11 = plotter.getPad();
		theAxes_11.setLabelSpace(0, 0.0);
		theAxes_11.setLabelSpace(1, 0.0);
		HPlotJaUtil.setTickLabelZeroPositive(theAxes_11);

		JaText labX = plotter.getLabelX()[0][0];
		labX.setFont(axisLabelFont);
		HPlotJaUtil.setLabelLocationAtCenter(0, labX, plotter, 0.025);
		JaText labY = plotter.getLabelY()[0][0];
		labY.setFont(axisLabelFont);
		HPlotJaUtil.setLabelLocationAtCenter(1, labY, plotter, -0.01);

		plotter.update();

		if (writeFigures)
		{
			for (int i = 0; i < figType.length; i++)
			{
				//String outFileName = title + figType[i];
				plotter.export(outPath + File.separator + "Spectrum" + title.split("at ")[1] + figType[i]);
				System.out.println("Writting File : " + outPath + File.separator + title + figType[i]);
			}
		}

		plotter.update();
	}
}

